import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite';
import Pages from 'vite-plugin-pages'
import path from 'path'
import config from './src/config';
import Components from 'unplugin-vue-components/vite'

import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'



const resolve = (root, src) => {
	return path.resolve(root, src)
}


// https://vitejs.dev/config/
export default defineConfig(({ command, mode, ssrBuild }) => {
	console.log(`./${ssrBuild}.html`);
	return {

		server: {
			open: true
		},
		plugins: [
			vue(),
			Pages({
				// dirs: 'src/views',
				dirs: [{
					dir: "src/pages",
					baseRoute: "/"
				}],
				exclude: ["**/components/*.vue"], // 排除在外的目录，即不将所有 components 目录下的 .vue 文件生成路由
				extendRoute(route) {
					if (route.path === "/") return {
						...route,
						redirect: config.homePage
					}
				}
			}),
			AutoImport({
				resolvers: [ElementPlusResolver()],

				imports: [
					'vue',
					'pinia',
					'vue-router',
					{
						'@/utils/auth': ['getToken', 'setToken', 'removeToken'],
						'@/api/index': ['axiosReq'],
						'@/utils/request': ['request']
					}
				],
				eslintrc: {
					enabled: true, // Default `false`
					filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
					globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
				},
				dts: true //auto generation auto-imports.d.ts file
			}),
			Components({
				resolvers: [ElementPlusResolver()],
			}),
		],
		resolve: {
			alias: {
				'c': resolve(__dirname, 'src/components'),
				'~': resolve(__dirname, 'src'),
				'@': resolve(__dirname, 'src')
			},
		},
		build: {
			rollupOptions: {
				input: {
					index: path.resolve(__dirname, 'index.html'),
				}, output: {
					chunkFileNames: 'static/js/[name]-[hash].js',
					entryFileNames: "static/js/[name]-[hash].js",
					assetFileNames: "static/[ext]/name-[hash].[ext]"
				}
			},
		}
	}

})
